﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CoinsLibrary
{
    public class AexCommon
    {

        public static OrderResult Buy(string market, double price, double amount, string coinname)
        {
            var str = Aex.SubmitOrder("1",market,price,amount,coinname);
            var orderResult = GetOrderByString(str);
            orderResult.type = "1";
            orderResult.market = market;
            orderResult.price = price;
            orderResult.coinname = coinname;
            orderResult.amount = amount;
            orderResult.time = DHTools.GetTimeStamp();
            return orderResult;
        }
        public static OrderResult Sell(string market, double price, double amount, string coinname)
        {
            var str = Aex.SubmitOrder("2", market, price, amount, coinname);
            var orderResult = GetOrderByString(str);
            orderResult.type = "2";
            orderResult.market = market;
            orderResult.price = price;
            orderResult.coinname = coinname;
            orderResult.amount = amount;
            orderResult.time = DHTools.GetTimeStamp();
            return orderResult;
        }
        public static OrderResult GetOrderByString(string str)
        {
            DHTools.SetLog(str, "order.log");
            OrderResult coinOrder = new OrderResult();
            if (str.IndexOf("overBalance") >= 0)
            {
                coinOrder.status = -1;
                return coinOrder;
            }
            Regex regex = new Regex("succ$");
            if (regex.IsMatch(str))
            {
                coinOrder.status = 2;
                return coinOrder;
            }
            regex = new Regex("succ\\|(\\d+)");
            if (regex.IsMatch(str))
            {
                coinOrder.status = 1;
                coinOrder.oid = regex.Match(str).Groups[1].Value;
                return coinOrder;
            }
            regex = new Regex("(\\d+\\.\\d+\\.\\d+\\.).+is not allowed");
            if (regex.IsMatch(str))
            {
                coinOrder.status = -2;
                coinOrder.oid = regex.Match(str).Groups[1].Value;
                return coinOrder;
            }
            DHTools.SetLog(str, "err.log");
            return coinOrder;
        }
        public static void SaveBalance(string coin_name)
        {
            string json = "";
            try
            {
                json = Aex.GetBalance();
                //DHTools.SetLog(json);
                var balance = JObject.Parse(json);
                var cnc = balance["cnc_balance"].ToObject<double>();
                var cnc_lock = balance["cnc_balance_lock"].ToObject<double>();

                var usdt = balance["usdt_balance"].ToObject<double>();
                var usdt_lock = balance["usdt_balance_lock"].ToObject<double>();

                var coin = balance[coin_name + "_balance"].ToObject<double>();
                var coin_lock = balance[coin_name + "_balance_lock"].ToObject<double>();
                Console.WriteLine(cnc);
                Console.WriteLine(usdt);
                Console.WriteLine(coin);
                string str = string.Format("{0} , {1}; {2} , {3}; {4} , {5}",cnc,cnc_lock,usdt,usdt_lock,coin,coin_lock);
                DHTools.SetLog(str, "balance.txt");
            }
            catch
            {
                Console.WriteLine(json);
            }
        }
        public static List<OrderResult> GetOrderList(string mk_type,string coin_name)
        {
            List<OrderResult> list = new List<OrderResult>();
            try
            {
                var orders = Aex.GetOrderList(mk_type, coin_name);
                //Console.WriteLine(orders);
                JArray ja = JArray.Parse(orders);
                foreach(var j in ja)
                {
                    var order = new OrderResult
                    {
                        oid = j["id"].ToString(),
                        market=mk_type,
                        coinname=coin_name,
                        type=j["type"].ToString()
                    };
                    list.Add(order);
                }
            }
            catch(Exception ex)
            {
                //Console.WriteLine(ex.Message);
            }
            
            return list;
        }
        public static CoinPrice GetPrice(string coin_name, string market)
        {
            var json = Aex.GetDepth(coin_name, market);
            if (json.Length<10)
            {
                throw new Exception("return lenght:"+json.Length);
            }
            try
            {
                JObject jObject = JObject.Parse(json);
                double bid = jObject["bids"][0][0].ToObject<double>();
                double ask = jObject["asks"][0][0].ToObject<double>();
                double bidAmount = jObject["bids"][0][1].ToObject<double>();
                double askAmount = jObject["asks"][0][1].ToObject<double>();
                return new CoinPrice
                {
                    symbol = coin_name + market,
                    bid = bid,
                    ask = ask,
                    bidAmount = bidAmount,
                    askAmount = askAmount,
                    time = DHTools.GetTimeStamp()
                };
            }
            catch
            {
                throw new Exception("get_price_err:"+json);
            }
            
        }
    }
}
